<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{ this.currentCity }}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门成</div>
        <div class="button-list">
          <div
            class="button-wrapper"
            @click="handleCityClick(item.name)"
            v-for="item in hotCities"
            :key="item.id"
          >
            <div class="button">{{ item.name }}</div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(item, key) in cities" :key="key" :ref="key">
        <div class="title border-topbottom">{{ key }}</div>
        <div class="item-list">
          <div
            class="item border-topbottom"
            v-for="itemname in item"
            :key="itemname.id"
            @click="handleCityClick(itemname.name)"
          >
            {{ itemname.name }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import Bscroll from "better-scroll";
import { mapState, mapMutations } from "vuex";
export default {
  name: "cityList",
  props: {
    hotCities: Array,
    cities: Object,
    letter: String,
  },
  computed: {
    ...mapState({ currentCity: "city" }),
  },
  watch: {
    letter() {
      if (this.letter) {
        const element = this.$refs[this.letter][0];
        this.scroll.scrollToElement(element);
      }
    },
  },

  mounted() {
    this.scroll = new Bscroll(this.$refs.wrapper);
  },
  updated() {
    //重新计算高度
    this.scroll.refresh();
  },
  methods: {
    handleCityClick(city) {
      this.$store.commit("changeCity", city);
      this.$router.push("/");
    },
  },
};
</script>
<style lang="stylus" scoped>
@import '~style/var.styl'
	.border-topbottom {
  &:before {
    border-color: #ccc;
  }

  &:after {
    border-color: #ccc;
  }
}

.border-bottom {
  &:before {
    border-color: #ccc;
  }
}
.list
	overflow hidden
	position: absolute
	left 0
	top 1.58rem
	right 0
	bottom 0
	.title
		line-height: 0.54rem
		background: #eee
		padding-left: 0.2rem
		color: #666
		font-size: 0.26rem
		text-align left
	.button-list
		padding .1rem
		overflow hidden
		.button-wrapper
			width 33.33%
			float left
			.button
				text-align center
				border-radius .1rem
				border: 0.02rem solid #ccc
				margin: 0.06rem
				padding: 0.1rem 0
	.item-list

		.item
			line-height: .76rem
			padding-left: .4rem
			color:#666
			text-align left
</style>
